package cn.com.nxt.txb.httpdelegate;

import android.util.Log;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import cn.com.nxt.txb.HttpProtocol;
import cn.com.nxt.txb.ResponseStatus;
import cn.com.nxt.txb.model.BaseModel;
import cn.com.nxt.txb.util.AppConstant;

/**
 * 云会议室管理
 * Created by zhangling on 2017/6/6.
 */

public class MeetingDelegate {
    private HttpProtocol protocol;

    public MeetingDelegate(HttpProtocol protocol) {
        this.protocol = protocol;
    }

    public void meetingInfo(String token,String roomid, final byte flag) {
        RequestParams rp = new RequestParams(AppConstant.MEETING_INFO);
        rp.setAsJsonContent(true);
        rp.addQueryStringParameter("token", token);
        rp.addQueryStringParameter("roomid", roomid);
        // rp.setBodyContent(param);
        x.http().get(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                // Gson gson = new Gson();
                Log.v("--meetingInfo_success--", result);
                BaseModel model = BaseModel.parseResult(result);
                String code = model.getCode();
                if (code.equals("10000"))
                    protocol.success(model.getData(), flag);
                else
                    protocol.failure("请求失败，请重试", flag, ResponseStatus.REQUEST_FAILURE);
            }

            //请求异常后的回调方法
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("--meetingInfo_success_error--", ex.getMessage());
                protocol.failure("网络连接失败，请稍后重试", flag, ResponseStatus.NET_ERROR);
            }

            //主动调用取消请求的回调方法
            @Override
            public void onCancelled(CancelledException cex) {
                protocol.cancelled(flag);
            }

            @Override
            public void onFinished() {
                protocol.finished(flag);
            }
        });
    }

    public void setMeetingInfo(String token,String param, final byte flag) {
        //Log.v("--setMeetingInfo_param--", token+"--"+param);
        RequestParams rp = new RequestParams(AppConstant.MEETING_INFO);
        rp.setAsJsonContent(true);
        rp.setHeader("token", token);
        rp.setBodyContent(param);
        x.http().post(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                // Gson gson = new Gson();
                Log.v("--setMeetingInfo_success--", result);
                BaseModel model = BaseModel.parseResult(result);
                String code = model.getCode();
                if (code.equals("10000"))
                    protocol.success(model.getData(), flag);
                else
                    protocol.failure("请求失败，请重试", flag, ResponseStatus.REQUEST_FAILURE);
            }

            //请求异常后的回调方法
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("--setMeetingInfo_error--", ex.getMessage());
                protocol.failure("网络连接失败，请稍后重试", flag, ResponseStatus.NET_ERROR);
            }

            //主动调用取消请求的回调方法
            @Override
            public void onCancelled(CancelledException cex) {
                protocol.cancelled(flag);
            }

            @Override
            public void onFinished() {
                protocol.finished(flag);
            }
        });
    }

    public void meetingSettingInfo(String token, final byte flag) {
        RequestParams rp = new RequestParams(AppConstant.MEETING_SETTING_INFO);
        rp.setAsJsonContent(true);
        rp.addQueryStringParameter("token", token);
        // rp.setBodyContent(param);
        x.http().get(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                // Gson gson = new Gson();
                Log.v("--meetingSettingInfo_success--", result);
                BaseModel model = BaseModel.parseResult(result);
                String code = model.getCode();
                if (code.equals("10000"))
                    protocol.success(model.getData(), flag);
                else
                    protocol.failure("请求失败，请重试", flag, ResponseStatus.REQUEST_FAILURE);
            }

            //请求异常后的回调方法
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("--meetingSettingInfo_error--", ex.getMessage());
                protocol.failure("网络连接失败，请稍后重试", flag, ResponseStatus.NET_ERROR);
            }

            //主动调用取消请求的回调方法
            @Override
            public void onCancelled(CancelledException cex) {
                protocol.cancelled(flag);
            }

            @Override
            public void onFinished() {
                protocol.finished(flag);
            }
        });
    }

    public void setMeetingSettingInfo(String token,String param, final byte flag) {
        Log.v("--setMeetingSettingInfo_param--", token+"--"+param);
        RequestParams rp = new RequestParams(AppConstant.MEETING_SETTING_INFO);
        rp.setAsJsonContent(true);
        rp.setHeader("token", token);
        rp.setBodyContent(param);
        x.http().post(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                // Gson gson = new Gson();
                Log.v("--setMeetingSettingInfo_success--", result);
                BaseModel model = BaseModel.parseResult(result);
                String code = model.getCode();
                if (code.equals("10000"))
                    protocol.success(model.getData(), flag);
                else
                    protocol.failure("请求失败，请重试", flag, ResponseStatus.REQUEST_FAILURE);
            }

            //请求异常后的回调方法
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("--setMeetingSettingInfo_error--", ex.getMessage());
                protocol.failure("网络连接失败，请稍后重试", flag, ResponseStatus.NET_ERROR);
            }

            //主动调用取消请求的回调方法
            @Override
            public void onCancelled(CancelledException cex) {
                protocol.cancelled(flag);
            }

            @Override
            public void onFinished() {
                protocol.finished(flag);
            }
        });
    }

    //5. 获取会议室是否进入权限
    public void meetingInAuth(String token,String roomid, final byte flag) {
        RequestParams rp = new RequestParams(AppConstant.MEETING_IN_AUTH);
        rp.setAsJsonContent(true);
        rp.addQueryStringParameter("token", token);
        rp.addQueryStringParameter("roomid", roomid);
        x.http().get(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                // Gson gson = new Gson();
                Log.v("--meetingInAuth_success--", result);
                BaseModel model = BaseModel.parseResult(result);
                String code = model.getCode();
                if (code.equals("10000"))
                    protocol.success(model.getData(), flag);
                else
                    protocol.failure("请求失败，请重试", flag, ResponseStatus.REQUEST_FAILURE);
            }

            //请求异常后的回调方法
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("--meetingInAuth_error--", ex.getMessage());
                protocol.failure("网络连接失败，请稍后重试", flag, ResponseStatus.NET_ERROR);
            }

            //主动调用取消请求的回调方法
            @Override
            public void onCancelled(CancelledException cex) {
                protocol.cancelled(flag);
            }

            @Override
            public void onFinished() {
                protocol.finished(flag);
            }
        });
    }
    //6. 检验入会权限
    public void meetingInCheck(String token,String password, final byte flag) {
        Log.v("--meetingInCheck_param--", token+"--"+password);
        RequestParams rp = new RequestParams(AppConstant.MEETING_CHECK);
        rp.setAsJsonContent(true);
        rp.setHeader("token", token);
        rp.setHeader("password",password);
        x.http().post(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                // Gson gson = new Gson();
                Log.v("--meetingInCheck_success--", result);
                BaseModel model = BaseModel.parseResult(result);
                String code = model.getCode();
                if (code.equals("10000"))
                    protocol.success(model.getData(), flag);
                else
                    protocol.failure("请求失败，请重试", flag, ResponseStatus.REQUEST_FAILURE);
            }

            //请求异常后的回调方法
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("--meetingInCheck_error--", ex.getMessage());
                protocol.failure("网络连接失败，请稍后重试", flag, ResponseStatus.NET_ERROR);
            }

            //主动调用取消请求的回调方法
            @Override
            public void onCancelled(CancelledException cex) {
                protocol.cancelled(flag);
            }

            @Override
            public void onFinished() {
                protocol.finished(flag);
            }
        });
    }


    //1. 检测会议是否开始
    public void isMeetingBegin(String token, final String roomid, final byte flag) {
        RequestParams rp = new RequestParams(AppConstant.MEETING_CHECK);
        rp.setAsJsonContent(true);
        rp.addQueryStringParameter("token", token);
        rp.addQueryStringParameter("roomid", roomid);
        x.http().get(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                // Gson gson = new Gson();
                Log.v("--isMeetingBegin_success--"+roomid, result);
                BaseModel model = BaseModel.parseResult(result);
                String code = model.getCode();
                if (code.equals("10000"))
                    protocol.success(model.getData(), flag);
                else if (code.equals("10009"))
                    protocol.failure("没有企业权限，不能创建会议", flag, ResponseStatus.CODE_MEETING_NOT_IN_BUSINESS);
                else if (code.equals("10007"))
                    protocol.failure("会议未开始", flag, ResponseStatus.CODE_MEETING_NOT_IN_BUSINESS);
                else
                    protocol.failure("请求失败，请重试", flag, ResponseStatus.REQUEST_FAILURE);
            }

            //请求异常后的回调方法
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("--isMeetingBegin_error--", ex.getMessage());
                protocol.failure("网络连接失败，请稍后重试", flag, ResponseStatus.NET_ERROR);
            }

            //主动调用取消请求的回调方法
            @Override
            public void onCancelled(CancelledException cex) {
                protocol.cancelled(flag);
            }

            @Override
            public void onFinished() {
                protocol.finished(flag);
            }
        });
    }
}
